package com.xbts.web.controller.editor;

import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.core.domain.entity.SysUser;
import com.xbts.common.core.page.TableDataInfo;
import com.xbts.common.enums.BusinessType;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.system.domain.SeiTemplate;
import com.xbts.system.domain.dto.InsertMaterialToTemplateDto;
import com.xbts.system.service.ISeiTemplateService;
import com.xbts.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模板表Controller
 *
 * @author xbts
 * @date 2023-08-17
 */
@Api("模板")
@RestController
@RequestMapping("/system/template")
public class SeiTemplateController extends BaseController {

    @Autowired
    private ISeiTemplateService seiTemplateService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查看模板表
     */
    @ApiOperation("查看模板")
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SeiTemplate seiTemplate) {
        startPage();
        return getDataTable(seiTemplateService.selectTemplateList(seiTemplate));
    }

    /**
     * 我的模板
     */
    @ApiOperation("我的模板列表")
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/my/list")
    public TableDataInfo myList(SeiTemplate seiTemplate) {
        startPage();
        seiTemplate.setCreateBy(SecurityUtils.getUserId().toString());
        List<SeiTemplate> list = seiTemplateService.selectTemplateList(seiTemplate);
        return getDataTable(list);
    }

    /**
     * 获取用户列表
     */
    @ApiOperation("获取用户列表")
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/user/list")
    public AjaxResult list()
    {
        SysUser user = new SysUser();
        user.setStatus("0");
        user.setAuditStatus("2");
        List<SysUser> list = userService.selectUserList(user);
        return success(list);
    }

    /**
     * 修改模板
     */
    @ApiOperation("修改模板")
    @PreAuthorize("@ss.hasPermi('system:template:update')")
    @Log(title = "模板", businessType = BusinessType.INSERT)
    @PostMapping("/update")
    public AjaxResult updateTemplate(@Validated @RequestBody SeiTemplate seiTemplate) {
        return toAjax(seiTemplateService.updateTemplate(seiTemplate));
    }

    /**
     * 新增模板类型
     */
    @ApiOperation("新增模板类型")
    @PreAuthorize("@ss.hasPermi('system:template:insert')")
    @Log(title = "模板", businessType = BusinessType.INSERT)
    @GetMapping("/insertType")
    public AjaxResult insertTemplateType(@Validated String name) {
        return toAjax(seiTemplateService.insertTemplateType(name));
    }

    /**
     * 新增模板
     */
    @ApiOperation("新增模板")
    @PreAuthorize("@ss.hasPermi('system:template:insert')")
    @Log(title = "模板", businessType = BusinessType.INSERT)
    @PostMapping("/insertTemplate")
    public AjaxResult insertTemplate( @RequestBody SeiTemplate seiTemplate) {
        return toAjax(seiTemplateService.insertTemplate(seiTemplate));
    }

    /**
     * 保存素材到模板
     */
    @ApiOperation("保存素材到模板")
    @PreAuthorize("@ss.hasPermi('system:template:insert')")
    @Log(title = "保存素材到模板", businessType = BusinessType.INSERT)
    @PostMapping("/save/material")
    public AjaxResult insertMaterialToTemplate( @RequestBody InsertMaterialToTemplateDto dto) {
        return toAjax(seiTemplateService.insertMaterialToTemplate(dto));
    }


    /**
     * 删除模板
     */
    @ApiOperation("删除模板")
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult deleteTemplate(@Validated String templateId) {
        return toAjax(seiTemplateService.deleteTemplate(templateId));
    }

    /**
     * 批量删除模板
     */
    @ApiOperation("批量删除模板")
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "模板", businessType = BusinessType.DELETE)
    @PostMapping("/batchDelete")
    public AjaxResult batchDeleteTemplate(@RequestBody List<String> ids) {
        return toAjax(seiTemplateService.batchDeleteTemplate(ids));
    }

    /**
     * 获取模板详细信息
     */
    @ApiOperation("获取模板详细信息")
    @ApiImplicitParam(name = "templateId", value = "模板ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") String templateId) {
        return success(seiTemplateService.selectSeiByTemplateId(templateId));
    }

    /**
     * 生成word
     */
    @PreAuthorize("@ss.hasPermi('system:template:insert')")
    @ApiOperation("生成word")
    @PostMapping("/word")
    public void generateWord(HttpServletRequest request, HttpServletResponse response, @RequestBody SeiTemplate template) {
        seiTemplateService.outPutWord(request, response, template.getTemplate());
    }

    @ApiOperation("导入word")
    @PreAuthorize("@ss.hasPermi('system:template:insert')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {

        return success(seiTemplateService.importData(file));
    }
}
